package com.miao.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.miao.common.entity.pojo.Product;
import com.miao.common.entity.pojo.Store;
import com.miao.shop.entity.ShopCar;
import com.miao.shop.entity.ShopCarProduct;
import com.miao.shop.entity.vo.OrderVO;
import com.miao.shop.service.ShopCarService;
import com.miao.shop.service.ShopProductService;
import com.miao.shop.service.ShopStoreService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author MiaoWenqing
 * @description: 购物车 实现
 * @date 2021/2/16 22:15
 */
@Service
public class ShopCarServiceImpl  implements ShopCarService {



    @Resource(name = "myRedisTemplate")
    private RedisTemplate<String ,Object> redisTemplate;


    @Resource
    private ShopProductService shopProductService;

    @Resource
    private ShopStoreService shopStoreService;


    /**
     * 添加商品
     *
     * @param pId 商品id
     * @param sId 店铺id
     * @param uId 用户id
     * @return boolean
     */
    @Override
    public boolean addProduct(String pId, String sId, String uId,int num) {


        //先去redis 检查当前用户是否拥有购物车
        List<ShopCar> car = this.getCar(uId);

        //没有 就创建一个
        if (null==car){
            car=new ArrayList<ShopCar>();


        }


        //获取当前商品信息
        Product product = this.shopProductService.getById(pId);
        ShopCarProduct shopCarProduct = new ShopCarProduct();
        shopCarProduct.setpIcon(product.getProIcon());
        shopCarProduct.setpId(product.getProId());
        shopCarProduct.setpName(product.getProName());
        shopCarProduct.setpNum(num);


        //判断商品当前价格
        if (product.getIsDiscount()){
            shopCarProduct.setpPrice(product.getProPrice().subtract(BigDecimal.valueOf(product.getProDis())));
        }else {
            shopCarProduct.setpPrice(product.getProPrice());
        }
        //首次加入选中
        shopCarProduct.setSelect(true);

        //将商品加入店铺
        boolean isAdd=false;
        for (ShopCar shopCar : car) {
            if (shopCar.getsId().equals(sId)){
                isAdd=true;
                shopCar.getProductList().add(shopCarProduct);
                break;
            }
        }

        //要添加的商品的店铺不存在
        if (!isAdd){
            Store store = this.shopStoreService.getById(sId);
            ShopCar shopCar = new ShopCar();
            shopCar.setsId(String.valueOf(store.getStoreId()));
            shopCar.setsName(store.getStoreName());
            shopCar.setProductList(new HashSet<ShopCarProduct>());
            shopCar.getProductList().add(shopCarProduct);
            car.add(shopCar);
        }



        this.addCar(uId,car);


        return true;
    }

    /**
     * 删除一个商品
     *
     * @param pId 商品id
     * @param sId 店铺id
     * @param uId 用户id
     * @return boolean
     */
    @Override
    public boolean delProduct(String pId, String sId, String uId) {

        List<ShopCar> car = this.getCar(uId);
        if (null==car){
            return false;
        }else {
            for (ShopCar shopCar : car) {
                if (shopCar.getsId().equals(sId)){
                    for (ShopCarProduct shopCarProduct : shopCar.getProductList()) {
                        if (shopCarProduct.getpId().equals(pId)){
                            shopCar.getProductList().remove(shopCarProduct);
                            if (shopCar.getProductList().size()==0){
                                car.remove(shopCar);
                            }
                            this.addCar(uId,car);
                            return  true;
                        }
                    }

                }
            }
        }



        return false;
    }


    /**
     * 修改商品数量
     *
     * @param pId 商品id
     * @param sId 店铺id
     * @param uId 用户id
     * @param num 数量
     * @return boolean
     */
    @Override
    public boolean editNum(String pId, String sId, String uId, int num) {
        List<ShopCar> car = this.getCar(uId);
        if (null==car){
            return false;
        }else {
            for (ShopCar shopCar : car) {
                if (shopCar.getsId().equals(sId)){
                    for (ShopCarProduct shopCarProduct : shopCar.getProductList()) {
                        if (shopCarProduct.getpId().equals(pId)){
                            shopCarProduct.setpNum(num);
                            shopCar.getProductList().add(shopCarProduct);
                            this.addCar(uId,car);
                            return true;
                        }
                    }

                }
            }
        }
        return false;
    }

    /**
     * 修改选中状态
     *
     * @param pId 商品id
     * @param sId 店铺id
     * @param uId 用户id
     * @return boolean
     */
    @Override
    public boolean editSelect(String pId, String sId, String uId) {
        List<ShopCar> car = this.getCar(uId);
        if (null==car){
            return false;
        }else {
            for (ShopCar shopCar : car) {
                if (shopCar.getsId().equals(sId)){
                    for (ShopCarProduct shopCarProduct : shopCar.getProductList()) {
                        if (shopCarProduct.getpId().equals(pId)){

                            shopCarProduct.setSelect(!shopCarProduct.isSelect());
                            shopCar.getProductList().add(shopCarProduct);
                            this.addCar(uId,car);
                            return true;
                        }
                    }

                }
            }
        }
        return false;
    }


    /**
     * 添加购物车到redis
     * @param uId 用户id
     * @param shopCars 购物车
     * @return boolean
     */
    public boolean addCar( String uId,List<ShopCar> shopCars){
        this.redisTemplate.opsForHash().put("car",uId, JSON.toJSONString(shopCars));
        return true;
    }


    /**
     * 根据用户id 取购物车
     * @param uId uid
     * @return  boolean
     */
    public List<ShopCar> getCar(String uId){
        String car = (String) this.redisTemplate.opsForHash().get("car", uId);
        List list = JSON.parseObject(car, List.class);
        if (null==list){
            return null;
        }
        List<ShopCar> shopCars = new ArrayList<>();
        list.forEach(item->{
            ShopCar shopCar = JSON.parseObject(JSON.toJSONString(item), ShopCar.class);
            shopCars.add(shopCar);
        });

        return  shopCars;
    }







}
